<?php
namespace app\api\controller;

use think\Controller;
use think\Db;

class Base extends Controller
{
    protected $uid;
    protected $userData;
    public function __construct()
    {
        parent::__construct();
        $this->openid();
    }
    protected function openid()
    {
        $openid = request()->post('openid');
        //dump($openid);
      //return $openid;die;
        if ($openid == null) {
            $this->AjaxReturn(-1, "用户信息错误！");
        }
        else{
            $data = Db::name('user')->where('code',$openid)->find();
            if (empty($data)){
                $res = Db::name('user')->insert([
                    'code'=>$openid,
                    'uflg'=>1,
                  	'gflg'=>0,
                  	"avatars_img" => '',
                  	"phone" => '',
                  	'uname' => '',
                  "company" => ''
                ]);
                if($res){
					$this->AjaxReturn(-2, "您还没有注册，请及时注册");
				}
            }else{
				if($data['phone'] == ''){
					$this->AjaxReturn(-2, "您还没有注册，请及时注册");
				}else{
					$this->uid = $data['uid'];
					$this->openid=$openid;
				}
            }
        }
    }
	public function AjaxReturn($code,$msg,$data=array())
	{

		$returnData=array(
			"code"=>$code
		);
		if($msg)
		{
		  $returnData["message"]=$msg;
		}else
		{
			$returnData["message"]=$this->getErrorInfo($code);
		}
		if(!empty($data))
		{
			$returnData["data"]=$data;
		}
		exit(json_encode($returnData));
	}
	//获取code对应值
	public function getErrorInfo($code)
	{
		  $returnData=Db::name("system_code")->where("code",$code)->find();
		  if(empty($returnData))
		  {
			  return "操作失败！";
		  }else
		  {
			  return $returnData["message"];
		  }
	}
}